package com.zjtaixue.usersite.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.zjtaixue.usersite.domain.Class;
import com.zjtaixue.usersite.domain.Major;
import com.zjtaixue.usersite.domain.Teacher;
import com.zjtaixue.usersite.dto.ClassInfoDto;
import com.zjtaixue.usersite.exception.ApplicationException;
import com.zjtaixue.usersite.mapper.*;
import com.zjtaixue.usersite.page.PageResult;
import com.zjtaixue.usersite.query.ClassQueryObject;
import com.zjtaixue.usersite.service.ClassService;
import lombok.extern.slf4j.Slf4j;
import org.apache.lucene.util.ArrayUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

@Slf4j
@Service("classService")
public class ClassServiceImpl implements ClassService {
    @Autowired
    private ClassMapper classMapper;
    @Autowired
    private MajorMapper majorMapper;
    @Autowired
    private CollegeMapper collegeMapper;
    @Autowired
    private StudentMapper studentMapper;
    @Autowired
    private TeacherMapper teacherMapper;



    @Override
    public PageResult<JSONObject> listPageResult(ClassQueryObject qo) {
        int count = classMapper.queryCount(qo);
        if (count > 0) {
            List<Class> classes = classMapper.query(qo);
            List<JSONObject> jsonObjects = new ArrayList<>();
            for (Class cla : classes) {
                JSONObject jsonObject = (JSONObject) JSON.toJSON(cla);
                Major major = majorMapper.selectByPrimaryKey(cla.getMajorId());
                jsonObject.put("major", major);
                jsonObject.put("college", collegeMapper.selectByPrimaryKey(major.getCollegeId()));
                jsonObjects.add(jsonObject);
            }
            return new PageResult<JSONObject>(jsonObjects, qo.getCurrentPage(), count, qo.getPageSize());
        }
        return PageResult.empty(qo.getPageSize());
    }

    @Override
    public int insert(Class cla) {
        int count = classMapper.countByName(cla.getName());
        if (count > 0) {
            throw new ApplicationException("已存在相同名称的班级");
        }
        return classMapper.insert(cla);
    }

    @Override
    public Class getById(Integer id) {
        return classMapper.selectByPrimaryKey(id);
    }

    @Override
    public int deleteById(Integer id) {
        int count = studentMapper.countByClassId(id);
        if (count > 0) {
            Class cla = classMapper.selectByPrimaryKey(id);
            throw new ApplicationException(cla.getName() + "已存在学生，不能删除！");
        }
        return classMapper.deleteByPrimaryKey(id);
    }

    @Override
    public void update(Class cla) {
        int count = classMapper.countOthersByName(cla);
        if (count > 0) {
            throw new ApplicationException("已存在相同名称的班级！");
        }
        classMapper.updateByPrimaryKey(cla);
    }

    @Override
    public List<Class> listByMajorId(Integer majorId) {
        return classMapper.listByMajorId(majorId);
    }
@Override
    public List<Class> listByEnrollmentYear(Integer enrollmentYear) {
        return classMapper.listByYear(enrollmentYear);
    }

    @Override
    public List<Integer> listAllEnrollmentYears() {
        return classMapper.listAllYears();
    }

    @Override
    public List<Class> listAll() {
        return classMapper.selectAll();
    }

    /**
     * 通过教师账号获取班级信息
     * @param teacherId
     * @return
     */
    @Override
    public String listByTeacherId(Integer teacherId) {
        List<ClassInfoDto>classInfoDtoList=new ArrayList<>();
        Teacher teacher =teacherMapper.selectByPrimaryKey(teacherId);
        List<Major>majorList=majorMapper.listByCollegeId(teacher.getCollegeId());
        for (int i = 0; i < majorList.size(); i++) {
            List<Class> classes=classMapper.listByMajorId(majorList.get(i).getId());
            for (int j = 0; j < classes.size(); j++) {
                ClassInfoDto classInfoDto=new ClassInfoDto();
                classInfoDto.setClassname(classes.get(j).getName());
                classInfoDto.setClassid(classes.get(j).getId());
                classInfoDto.setMajorname(majorList.get(i).getName());
                classInfoDtoList.add(classInfoDto);
            }
        }
        log.info("班级信息："+JSONObject.toJSONString(classInfoDtoList));
        return  JSONObject.toJSONString(classInfoDtoList);
    }
}
